package com.hontech.kaltaapp.device

import com.hontech.kaltaapp.device.codec.CodecType
import com.hontech.kaltaapp.utils.xorSum
import java.util.concurrent.atomic.AtomicInteger

object Proto {

    const val HEAD0 = 0xE1
    const val HEAD1 = 0x1E
    const val HEAD = 0xE11E
    const val END = 0xEF

    private val globalSeq = AtomicInteger(0)

    fun make(type: Int, seq: Int, vararg args: CodecType): ByteArray {
        var size = 7
        for (arg in args) {
            size += arg.size()
        }
        val buf = ByteArray(size)
        buf[0] = HEAD0.toByte()
        buf[1] = HEAD1.toByte()
        buf[2] = size.toByte()
        buf[3] = type.toByte()
        buf[4] = seq.toByte()
        var index = 5
        for (arg in args) {
            arg.encode(buf, index)
            index += arg.size()
        }
        val sum = xorSum(buf, 3, size - 5)
        buf[size - 2] = sum.toByte()
        buf[size - 1] = END.toByte()
        return buf
    }

    fun createSeq(): Int {
        return globalSeq.getAndAdd(1) and 0xFF
    }
}

object Type {

    const val ACK = 0
    const val PING = 1
    const val PONG = 2
    const val SIMPLE_REQ = 3
    const val SIMPLE_RES = 4
    const val SESSION = 5
    const val REQ = 6
    const val RES = 7
    const val NOTIFY = 8
    const val NOTIFY_ACK = 9
}


object Cmd {

    const val STATUS = 0
    const val LOG = 1
    const val OTA_START = 2
    const val OTA_TRANSLATE = 3
    const val OTA_COMPLETE = 4
    const val GET_TASK = 5
    const val PARTS_CTRL = 6
    const val NOZZLE_CTRL = 7
    const val DOOR_CTRL = 8
    const val SCAN = 9
    const val PARAM = 0x0A
    const val DRAW_HEAT_CTRL = 0x0B
    const val DRAW_WATER = 0x0C
    const val DRAW_WATER2 = 0x0D
}





